#!/bin/bash

# Maxwell连接测试脚本
# 检查maxwell用户的连接和权限配置

echo "=== Maxwell连接测试脚本 ==="
echo "时间: $(date)"
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 配置变量
MYSQL_HOST="192.168.10.102"
MYSQL_PORT="3306"
ROOT_PASSWORD="000000"
MAXWELL_PASSWORD="maxwell"

echo -e "${BLUE}1. 测试root用户连接${NC}"
echo "----------------------------------------"

# 测试root连接
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SELECT 1;" 2>/dev/null; then
    echo -e "${GREEN}✓ Root用户连接成功${NC}"
else
    echo -e "${RED}✗ Root用户连接失败${NC}"
    exit 1
fi

echo ""
echo -e "${BLUE}2. 检查MySQL配置${NC}"
echo "----------------------------------------"

# 检查binlog状态
echo "检查binlog状态..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW VARIABLES LIKE 'log_bin';" 2>/dev/null

# 检查binlog格式
echo "检查binlog格式..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW VARIABLES LIKE 'binlog_format';" 2>/dev/null

# 检查server_id
echo "检查server_id..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW VARIABLES LIKE 'server_id';" 2>/dev/null

echo ""
echo -e "${BLUE}3. 检查maxwell用户状态${NC}"
echo "----------------------------------------"

# 检查maxwell用户是否存在
echo "检查maxwell用户是否存在..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SELECT user, host FROM mysql.user WHERE user='maxwell';" 2>/dev/null

# 检查maxwell用户权限
echo "检查maxwell用户权限..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW GRANTS FOR 'maxwell'@'%';" 2>/dev/null || \
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW GRANTS FOR 'maxwell'@'localhost';" 2>/dev/null || \
echo "未找到maxwell用户权限信息"

echo ""
echo -e "${BLUE}4. 测试maxwell用户连接${NC}"
echo "----------------------------------------"

# 测试maxwell连接
echo "测试maxwell用户连接..."
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SELECT 1;" 2>/dev/null; then
    echo -e "${GREEN}✓ Maxwell用户连接成功${NC}"
else
    echo -e "${RED}✗ Maxwell用户连接失败${NC}"
    echo "错误信息："
    mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SELECT 1;" 2>&1 | head -5
fi

echo ""
echo -e "${BLUE}5. 创建maxwell用户（如果需要）${NC}"
echo "----------------------------------------"

# 检查是否需要创建maxwell用户
if ! mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SELECT 1 FROM mysql.user WHERE user='maxwell';" 2>/dev/null | grep -q "1"; then
    echo "创建maxwell用户..."
    mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD << EOF
-- 删除已存在的maxwell用户
DROP USER IF EXISTS 'maxwell'@'%';
DROP USER IF EXISTS 'maxwell'@'localhost';

-- 创建maxwell用户
CREATE USER 'maxwell'@'%' IDENTIFIED BY '$MAXWELL_PASSWORD';

-- 授予必要权限
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'maxwell'@'%';
GRANT SELECT, INSERT, UPDATE, DELETE ON maxwell.* TO 'maxwell'@'%';

-- 创建maxwell数据库（如果不存在）
CREATE DATABASE IF NOT EXISTS maxwell;

-- 刷新权限
FLUSH PRIVILEGES;

-- 验证用户创建
SELECT user, host FROM mysql.user WHERE user='maxwell';
EOF

    echo ""
    echo "重新测试maxwell连接..."
    if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SELECT 1;" 2>/dev/null; then
        echo -e "${GREEN}✓ Maxwell用户创建并连接成功${NC}"
    else
        echo -e "${RED}✗ Maxwell用户创建失败${NC}"
    fi
else
    echo -e "${GREEN}✓ Maxwell用户已存在${NC}"
fi

echo ""
echo -e "${BLUE}6. 检查maxwell数据库${NC}"
echo "----------------------------------------"

# 检查maxwell数据库
echo "检查maxwell数据库..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "SHOW DATABASES LIKE 'maxwell';" 2>/dev/null

# 检查maxwell数据库中的表
echo "检查maxwell数据库中的表..."
mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD -e "USE maxwell; SHOW TABLES;" 2>/dev/null

echo ""
echo -e "${BLUE}7. 测试Maxwell所需权限${NC}"
echo "----------------------------------------"

# 测试maxwell用户的各种权限
echo "测试SELECT权限..."
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SELECT 1;" 2>/dev/null; then
    echo -e "${GREEN}✓ SELECT权限正常${NC}"
else
    echo -e "${RED}✗ SELECT权限失败${NC}"
fi

echo "测试REPLICATION SLAVE权限..."
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SHOW MASTER STATUS;" 2>/dev/null; then
    echo -e "${GREEN}✓ REPLICATION SLAVE权限正常${NC}"
else
    echo -e "${RED}✗ REPLICATION SLAVE权限失败${NC}"
fi

echo "测试REPLICATION CLIENT权限..."
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "SHOW SLAVE HOSTS;" 2>/dev/null; then
    echo -e "${GREEN}✓ REPLICATION CLIENT权限正常${NC}"
else
    echo -e "${YELLOW}⚠ REPLICATION CLIENT权限测试失败（可能没有从库）${NC}"
fi

echo "测试maxwell数据库写入权限..."
if mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD -e "USE maxwell; CREATE TABLE IF NOT EXISTS test_table (id INT); DROP TABLE test_table;" 2>/dev/null; then
    echo -e "${GREEN}✓ maxwell数据库写入权限正常${NC}"
else
    echo -e "${RED}✗ maxwell数据库写入权限失败${NC}"
fi

echo ""
echo -e "${BLUE}8. 连接信息总结${NC}"
echo "----------------------------------------"

echo "MySQL连接信息："
echo "  主机: $MYSQL_HOST"
echo "  端口: $MYSQL_PORT"
echo "  Root密码: $ROOT_PASSWORD"
echo "  Maxwell密码: $MAXWELL_PASSWORD"
echo ""
echo "连接命令："
echo "  Root: mysql -h $MYSQL_HOST -P $MYSQL_PORT -u root -p$ROOT_PASSWORD"
echo "  Maxwell: mysql -h $MYSQL_HOST -P $MYSQL_PORT -u maxwell -p$MAXWELL_PASSWORD"

echo ""
echo "=== 测试完成 ===" 